package com.develop.configuration;

import com.develop.data.base.model.Resource;
import com.develop.data.loan.repository.LoanContractRepository;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import java.net.InetAddress;

@Slf4j
@Data
@Configuration
public class AppConfig implements ApplicationListener<WebServerInitializedEvent> {

    @Bean
    @Primary
    public CacheManager appCacheManager() {
        return new ConcurrentMapCacheManager("menu");
    }

    /*通过hibernate查询出来的实体对象实际上都是代理对象，在序列化的时候，我们可能会遇到懒加载导致jackson无法正确解析对象的问题*/
    @Bean
    public Hibernate5Module appHibernate5Module() {
        return new Hibernate5Module();
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer appJackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.indentOutput(true);
    }

    /*@Bean
    CommandLineRunner commandLineRunner() {
        return args -> {
            log.info("Preloading CommandLineRunner");
        };
    }

    @Bean
    ApplicationRunner applicationRunner() {
        return args -> {
            log.info("Preloading ApplicationRunner");
        };
    }*/

    @SneakyThrows
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        WebServer server = event.getWebServer();
        WebServerApplicationContext context = event.getApplicationContext();
        Environment env = context.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        int port = server.getPort();
        String contextPath = env.getProperty("server.servlet.context-path");
        contextPath = contextPath == null ? "": contextPath;

        log.info("Local: http://localhost:{}",port);
        log.info("External: http://{}:{}{}", ip, port, contextPath);

        String h2ConsolePath = env.getProperty("spring.h2.console.path");
        h2ConsolePath = h2ConsolePath == null ? "": h2ConsolePath;
        log.info("H2 Console Local: http://localhost:{}{}{}", port, contextPath, h2ConsolePath);
        log.info("H2 Console External: http://{}:{}{}{}", ip, port, contextPath, h2ConsolePath);


    }

}
